package com.gamecauca.android;

//import com.badlogic.gdx.Handler;
//import com.badlogic.gdx.Override;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.gamecauca.screen.GameCauCa;
import com.mobclix.android.sdk.MobclixAdView;
import com.mobclix.android.sdk.MobclixAdViewListener;
import com.mobclix.android.sdk.MobclixMMABannerXLAdView;

public class AndroidGame extends AndroidApplication implements MobclixAdViewListener {
	public static String TAG = "Class  AndroidGame=== ";
	//declare mobclix banner
	private MobclixMMABannerXLAdView adview_banner;
	RelativeLayout layout;
	RelativeLayout.LayoutParams adParams;
	boolean isAdsShow;

	public void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		log(TAG, "start method onCreate()");
		// Log.e(TAG, "hix hix ====================");
		// initialize(new GameCauCa(new
		// ActionResolverAndroid(AndroidGame.this)), false);

		layout = new RelativeLayout(this);
		// Do the stuff that initialize() would do for you
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

		// Create the libgdx View
		View gameView = initializeForView(new GameCauCa(new ActionResolverAndroid(AndroidGame.this)), false);
 		layout.addView(gameView);

		// Add the AdMob view
		adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

		isAdsShow = false;
		setContentView(layout);
		log(TAG, "end method onCreate()");
	}
	
	@Override
	protected void onStart() {
		super.onStart();  
//        this.AddMobclixBanner();
	}
	
	/** 
	 * @author bangnt_vfa
	 * ad mobclix banner
	 */
	protected void AddMobclixBanner() {  
		if (isNetworkAvailable()) {
			
			RelativeLayout layout = new RelativeLayout(this);  
			
			adview_banner = new MobclixMMABannerXLAdView(this);  
			adview_banner.addMobclixAdViewListener(this);  
			
			RelativeLayout.LayoutParams childParams = new RelativeLayout.LayoutParams(320, 50);
			childParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			childParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
			layout.addView(adview_banner, childParams);  
			
			RelativeLayout.LayoutParams params =  new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
			
			this.addContentView(layout, params);  
			
			adview_banner.getAd();  
			adview_banner.setRefreshTime(5000); 
		}
    } 
	
	/** 
	 * @author bangnt_vfa
	 * check network
	 * @return
	 */
	private boolean isNetworkAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
		return activeNetworkInfo != null;
	}

	@Override
	public void onSuccessfulLoad(MobclixAdView view) {
		Log.v("MobclixAdvertisingView", "The ad request was successful!");
		view.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void onCustomAdTouchThrough(MobclixAdView adView, String string) {
		Log.v("MobclixAdvertisingView", "The custom ad responded with '" + string + "' when touched!");
	}
	
	@Override
	public void onFailedLoad(MobclixAdView view, int errorCode) {
		Log.v("MobclixAdvertisingView", "The ad request failed with error code: " + errorCode);  
		view.setVisibility(View.GONE);
	}
	
	public void onAdClick(MobclixAdView adView) {
		Log.v("MobclixAdvertisingView", "Ad clicked!"); 
	}  
	
	@Override
	public String query() {
		return "query";
	}
	
	@Override
	public String keywords() {
		return null;
	}

}
